<template>
  <div class="app-container">
    <el-card>
        <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-input
            v-model="keyWord"
            placeholder="请输入想要查询的信息"
            @keyup.enter.native.prevent="searchTeacherByName"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="searchTeacherByName"
            >搜索</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="dialogAddVisible = true"
            icon="el-icon-plus"
            >添加</el-button
          >
          <el-dialog title="新增一个课程信息" :visible.sync="dialogAddVisible">
            <el-form @submit.native.prevent :model="teacherInfo">
              <el-form-item label="教师编号">
                <el-input v-model="teacherInfo.tId"></el-input>
              </el-form-item>
              <el-form-item label="教师名称">
                <el-input v-model="teacherInfo.name"></el-input>
              </el-form-item>
              <el-form-item label="性别">
                <el-select v-model="teacherInfo.sex" placeholder="请选择">
                  <el-option value="男">男</el-option>
                  <el-option value="女">女</el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="年龄">
                <el-input v-model="teacherInfo.age"></el-input>
              </el-form-item>
              <el-form-item label="电子邮箱">
                <el-input v-model="teacherInfo.email"></el-input>
              </el-form-item>
              <el-form-item label="电话号码">
                <el-input v-model="teacherInfo.telephone"></el-input>
              </el-form-item>
              <el-form-item label="所属院系">
                <el-select v-model="teacherInfo.deptId" placeholder="请选择">
                  <el-option
                    v-for="dept in depts"
                    :key="dept.deptId"
                    :label="dept.deptName"
                    :value="dept.deptId"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogAddVisible = false">取 消</el-button>
              <el-button type="primary" @click="addTeacherSubmit"
                >确 定</el-button
              >
            </div>
          </el-dialog>
        </el-form-item>
      </el-form>

      <!-- teacher table -->
      <el-table
        v-loading="listLoading"
        :data="teachers"
        element-loading-text="Loading"
        stripe
        height="550"
        border
        fits
        highlight-current-row
      >
        <el-table-column align="center" label="教师编号" prop="tId" />
        <el-table-column label="教师名称" align="center">
          <template slot-scope="{ row }">
            <el-input
              v-if="row.isEdit"
              v-model="row.name"
              class="edit-input"
              size="small"
            />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="性别" align="center">
          <template slot-scope="{ row }">
            <el-input
              v-if="row.isEdit"
              v-model="row.sex"
              class="edit-input"
              size="small"
            />
            <span v-else>{{ row.sex }}</span>
          </template>
        </el-table-column>
        <el-table-column label="年龄" align="center">
          <template slot-scope="{ row }">
            <el-input
              v-if="row.isEdit"
              v-model="row.age"
              class="edit-input"
              size="small"
            />
            <span v-else>{{ row.age }}</span>
          </template>
        </el-table-column>
        <el-table-column label="邮箱" width="200" align="center" prop="email">
          <template slot-scope="{ row }">
            <el-input
              v-if="row.isEdit"
              v-model="row.email"
              class="edit-input"
              size="small"
            />
            <span v-else>{{ row.email }}</span>
          </template>
        </el-table-column>
        <el-table-column label="电话号码" align="center" prop="telephone">
          <template slot-scope="{ row }">
            <el-input
              v-if="row.isEdit"
              v-model="row.telephone"
              class="edit-input"
              size="small"
            />
            <span v-else>{{ row.telephone }}</span>
          </template>
        </el-table-column>

        <el-table-column label="所属院系" width="180" align="center" prop="deptName">
          <template slot-scope="{ row }">
            <el-select v-if="row.isEdit" v-model="row.deptId" size="small">
              <el-option
                v-for="dept in depts"
                :key="dept.deptId"
                :label="dept.deptName"
                :value="dept.deptId"
              />
            </el-select>
            <span v-else>{{row.deptName}}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" prop="count">
          <template slot-scope="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="small"
              circle
              style="margin-left: 0"
              v-show="!row.isEdit"
              @click="row.isEdit = !row.isEdit"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-check"
              circle
              style="margin-left: 0"
              v-show="row.isEdit"
              size="small"
              @click="editOneTeacher(row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="small"
              circle
              @click="deleteOneTeacher(row.tId)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    
  </div>
</template>

<script>
import {
  getAllTeacher,
  addOneTeacher,
  updateOneTeacher,
  deleteOneTeacher,
  search,
} from "@/api/teacher";
export default {
  data() {
    return {
      teachers: null, //列表信息
      depts: null,
      listLoading: true, //是否加载中
      keyWord: "", //关键字
      dialogAddVisible: false,
      teacherInfo: {
        //教师信息
        tId: "",
        name: "",
        sex: "",
        age: "",
        email: "",
        telephone: "",
        deptId: "",
      },
    };
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getAllTeacher().then(({ data }) => {
        this.teachers = data.teachers.map((v) => {
          this.$set(v, "isEdit", false);
          return v;
        });
        this.depts = data.deptList;
        this.listLoading = false;
      });
    },
    addTeacherSubmit() {
      this.dialogAddVisible = false;
      addOneTeacher(this.teacherInfo).then((resp) => {
        this.$message({
          message: resp.msg,
          type: "success",
        });
        this.teachers.push(this.teacherInfo);
      });
    },
    editOneTeacher(row) {
      row.isEdit = false;
      updateOneTeacher(row).then((resp) => {
        this.$message({
          message: resp.msg,
          type: "success",
        });
      });
    },
    deleteOneTeacher(tId) {
      if (confirm("你确定要删除这条数据吗？"))
        deleteOneTeacher(tId).then((resp) => {
          this.$message({
            message: resp.msg,
            type: "success",
          });
          this.teachers = this.teachers.filter(item=>{
            return item.tId != tId;
          })
        });
    },
    searchTeacherByName() {
      search(this.keyWord).then((resp) => {
        this.teachers = resp.data.map((v) => {
          this.$set(v, "isEdit", false);
          return v;
        });
      });
    },
  },
  created() {
    this.fetchData();
  },
};
</script>
